package com.dao;
/***
*
*/
import com.util.*;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
public class OrderinfoDao {
	public OrderinfoDao(){
	
	}
	public int addOrderinfo(HttpServletRequest req){
		String orderid=req.getParameter("orderid")==null?"":req.getParameter("orderid");
		String userid=req.getParameter("userid")==null?"":req.getParameter("userid");
		String foodid=req.getParameter("foodid")==null?"":req.getParameter("foodid");
		String scid=req.getParameter("scid")==null?"":req.getParameter("scid");
		String isdefaut=req.getParameter("isdefaut")==null?"":req.getParameter("isdefaut");
		String price=req.getParameter("price")==null?"":req.getParameter("price");
		String sql ="insert into TBL_ORDERINFO("+
        		"ORDERID"+
        		",USERID"+
        		",FOODID"+
        		",SCID"+
        		",ISDEFAUT"+
        		",PRICE"+
        	") values("+
        		"'"+orderid+"'"+
        	        		",'"+userid+"'"+
        	        		",'"+foodid+"'"+
        	        		",'"+scid+"'"+
        	        		",'"+isdefaut+"'"+
        	        		",'"+price+"'"+
        	")";
        	DB db = new DB();
        	return db.executeSql(sql);	
	}
	public int delOrderinfo(HttpServletRequest req){
		String sql ="delete from TBL_ORDERINFO where 1=1 "+
    	"";
    	DB db = new DB();
    	return db.executeSql(sql);	
	}
	public int updOrderinfo(HttpServletRequest req){
		String orderid=req.getParameter("orderid")==null?"":req.getParameter("orderid");
		String userid=req.getParameter("userid")==null?"":req.getParameter("userid");
		String foodid=req.getParameter("foodid")==null?"":req.getParameter("foodid");
		String scid=req.getParameter("scid")==null?"":req.getParameter("scid");
		String isdefaut=req.getParameter("isdefaut")==null?"":req.getParameter("isdefaut");
		String price=req.getParameter("price")==null?"":req.getParameter("price");
		String sql ="update TBL_ORDERINFO set "+
        		"ORDERID='"+orderid+"'"+
        		",USERID='"+userid+"'"+
        		",FOODID='"+foodid+"'"+
        		",SCID='"+scid+"'"+
        		",ISDEFAUT='"+isdefaut+"'"+
        		",PRICE='"+price+"'"+
        	" where 1=1 "+
    	"";
        	DB db = new DB();
        	return db.executeSql(sql);	
	}
	public Map<String,Object> findOrderinfoById(HttpServletRequest req){
		String sql ="select * from TBL_ORDERINFO where 1=1 "+
    	"";
    	DB db = new DB();
    	Map<String,Object> map = null;
    	List<Map<String,Object>> list = db.query(sql);
    	if(list!=null&&list.size()>0){
    		map = list.get(0);
    	}
    	return map;	
	}
	
	
	public PageModel findOrderinfoByCondition(HttpServletRequest req){
		String orderid=req.getParameter("orderid")==null?"":req.getParameter("orderid");
		req.setAttribute("orderid", orderid);
		String userid=req.getParameter("userid")==null?"":req.getParameter("userid");
		req.setAttribute("userid", userid);
		String foodid=req.getParameter("foodid")==null?"":req.getParameter("foodid");
		req.setAttribute("foodid", foodid);
		String scid=req.getParameter("scid")==null?"":req.getParameter("scid");
		req.setAttribute("scid", scid);
		String isdefaut=req.getParameter("isdefaut")==null?"":req.getParameter("isdefaut");
		req.setAttribute("isdefaut", isdefaut);
		String price=req.getParameter("price")==null?"":req.getParameter("price");
		req.setAttribute("price", price);
		String sql ="";
		String where="";
		if(orderid!=null&&!"".equals(orderid)){
    		where+=" and ORDERID like '%"+orderid+"%'";
		}
		if(userid!=null&&!"".equals(userid)){
    		where+=" and USERID like '%"+userid+"%'";
		}
		if(foodid!=null&&!"".equals(foodid)){
    		where+=" and FOODID like '%"+foodid+"%'";
		}
		if(scid!=null&&!"".equals(scid)){
    		where+=" and SCID like '%"+scid+"%'";
		}
		if(isdefaut!=null&&!"".equals(isdefaut)){
    		where+=" and ISDEFAUT like '%"+isdefaut+"%'";
		}
		if(price!=null&&!"".equals(price)){
    		where+=" and PRICE like '%"+price+"%'";
		}
    	String countsql ="select count(*) from TBL_ORDERINFO where 1=1 "+where;
    	PageModel model = new PageModel();
		String tempPage = req.getParameter("currentPage");
		int currentPage = 1;
		try {
			currentPage = Integer.parseInt(tempPage);
		} catch (NumberFormatException e) {
			currentPage=1;
		}
		model.setCurrentPage(currentPage);
		String tempCount = req.getParameter("pageCount");
		int pageCount = 20;
		try {
			pageCount = Integer.parseInt(tempCount);
		} catch (NumberFormatException e) {
			pageCount=20;
		}
		model.setPageCount(pageCount);
		sql="select  * from TBL_ORDERINFO where 1=1 "+where+" order by  desc limit "+(currentPage-1)+","+pageCount+"";
    	DB db = new DB();
    	int count = Integer.parseInt(db.queryFirst(countsql));
    	model.setTotalCount(count);
    	if(count>0){
    		model.setRes(db.query(sql));
    	}
    	return model;	
	}
	
}